import 'package:flutter/foundation.dart';
import 'package:flutter/services.dart';

import 'audio_spectrum_platform_interface.dart';

typedef void EventHandler(dynamic event);

class MethodChannelAudioSpectrum extends AudioSpectrumPlatform {
  final EventChannel music_spectrum_channel =
      const EventChannel('flutter_music_plugin.event.music.spectrum');

  final EventChannel record_spectrum_channel =
      const EventChannel('flutter_music_plugin.event.record.spectrum');

  final methodChannel = const MethodChannel('audio_spectrum');

  final stopPlayerChannel = const MethodChannel('stop_player');
  final startRecordChannel = const MethodChannel('start_record');
  final stopRecordChannel = const MethodChannel('stop_record');

  @override
  getAudioSpectrum(String url) {
    methodChannel.invokeMethod<String>('audio_spectrum', {'url': url});
  }



  @override
  listenMusicSpectrum(EventHandler onEvent, EventHandler onError) {
    music_spectrum_channel
        .receiveBroadcastStream()
        .listen(onEvent, onError: onError);
  }

  @override
  listenRecordSpectrum(EventHandler onEvent, EventHandler onError) {
    record_spectrum_channel
        .receiveBroadcastStream()
        .listen(onEvent, onError: onError);
  }

  @override
  stopPlayer() {
    stopPlayerChannel.invokeMethod<String>('stop_player');
  }

  @override
  startRecord() {
    startRecordChannel.invokeListMethod('start_record');
  }

  @override
  stopRecord() {
    stopRecordChannel.invokeListMethod('stop_record');
  }
}
